package com.ruoyi.yuangong.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jinchuguanli.domain.Shangpin;
import com.ruoyi.jinchuguanli.domain.Xiaoshouchuku;
import com.ruoyi.jinchuguanli.domain.XiaoshouchukuReturn;
import com.ruoyi.jinchuguanli.service.impl.XiaoshouchukuServiceImpl;
import com.ruoyi.mainDoMain.benzhoupaihang;
import com.ruoyi.mainDoMain.riyingyee;
import com.ruoyi.mainDoMain.yueyingyee;
import com.ruoyi.yuangong.domain.Vpaiban;
import com.ruoyi.yuangong.domain.dayinjilu;
import com.ruoyi.yuangong.mapper.VpaibanMapper;
import com.ruoyi.yuangong.service.IVpaibanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * qiandaoService业务层处理
 *
 * @author ruoyi
 * @date 2023-11-08
 */
@Service
public class VpaibanServiceImpl implements IVpaibanService {
    @Autowired
    private VpaibanMapper vpaibanMapper;

    @Autowired
    private XiaoshouchukuServiceImpl xiaoshouchukuService;

    /**
     * 查询qiandao
     *
     * @param username qiandao主键
     * @return qiandao
     */
    @Override
    public Vpaiban selectVpaibanByUsername (String username) {
        return vpaibanMapper.selectVpaibanByUsername(username);
    }

    /**
     * 查询qiandao列表
     *
     * @param vpaiban qiandao
     * @return qiandao
     */
    @Override
    public List<Vpaiban> selectVpaibanList (Vpaiban vpaiban) {
        return vpaibanMapper.selectVpaibanList(vpaiban);
    }

    /**
     * 新增qiandao
     *
     * @param vpaiban qiandao
     * @return 结果
     */
    @Override
    public int insertVpaiban (Vpaiban vpaiban) {
        return vpaibanMapper.insertVpaiban(vpaiban);
    }

    /**
     * 修改qiandao
     *
     * @param vpaiban qiandao
     * @return 结果
     */
    @Override
    public int updateVpaiban (Vpaiban vpaiban) {
        return vpaibanMapper.updateVpaiban(vpaiban);
    }

    /**
     * 批量删除qiandao
     *
     * @param usernames 需要删除的qiandao主键
     * @return 结果
     */
    @Override
    public int deleteVpaibanByUsernames (String usernames) {
        return vpaibanMapper.deleteVpaibanByUsernames(Convert.toStrArray(usernames));
    }

    /**
     * 删除qiandao信息
     *
     * @param username qiandao主键
     * @return 结果
     */
    @Override
    public int deleteVpaibanByUsername (String username) {
        return vpaibanMapper.deleteVpaibanByUsername(username);
    }

    @Override
    public List<yueyingyee> selectYueYingYeE(String format) {
        String[] formatStr = format.split("-");
        String before = formatStr[0]+"-"+formatStr[1]+"-01";
        return vpaibanMapper.selectYueYingYeE(format,before);
    }

    @Override
    public List<riyingyee> selectRiYingYeE(String before,String format, String name) {
        if (StringUtils.isEmpty(before)) {
            before = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
        }
        if (StringUtils.isEmpty(format)) {
            format = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
        }
        return vpaibanMapper.selectRiYingYeE(before,format,name);
    }

    @Override
    public List<benzhoupaihang> selectBenZhouPaiHang(String today) {
        Xiaoshouchuku xiaoshouchuku = new Xiaoshouchuku();
        String before = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()-7*24*3600*1000));
        Map<String,Object> map = new HashMap<>();
        map.put("beginXiadanriqi",before);
        map.put("endXiadanriqi",today);
        xiaoshouchuku.setParams(map);
        List<XiaoshouchukuReturn> xiaoshouchukuReturnList = xiaoshouchukuService.selectXiaoshouchukuReturnList(xiaoshouchuku);

        List<benzhoupaihang> benzhoupaihangList = new ArrayList<>();

        for (XiaoshouchukuReturn xr:xiaoshouchukuReturnList
             ) {
            benzhoupaihang benzhoupaihang = new benzhoupaihang();
            benzhoupaihang.setDanhao(xr.getChukudanhao());
            benzhoupaihang.setRiqi(xr.getXiadanriqi());
            benzhoupaihang.setYewuyuan(xr.getYewuyuan());
            List<Integer> shulist = new ArrayList<>();
            List<String> minglist = new ArrayList<>();
            for (Shangpin s:xr.getShangpin()
                 ) {
//                if (!s.getShangpinleibie().equals("防火锁") &&
//                    !s.getShangpinleibie().equals("闭门器") &&
//                    !s.getShangpinleibie().equals("拉爆盖")) {
                    minglist.add(s.getShangpinming());
                    shulist.add(Math.toIntExact(s.getShangpinshuliang()));
//                }
            }
            benzhoupaihang.setShangpinming(minglist);
            benzhoupaihang.setShuliang(shulist);
            benzhoupaihangList.add(benzhoupaihang);
        }
        benzhoupaihangList.sort((o1, o2) -> {
            int o1shu = 0, o2shu = 0;
            for (int count : o1.getShuliang()
            ) {
                o1shu += count;
            }
            for (int count : o2.getShuliang()
            ) {
                o2shu += count;
            }
            return o2shu - o1shu;
        });
        List<benzhoupaihang> benzhouList = new ArrayList<>();
        int count = 0;
        for (benzhoupaihang bz:benzhoupaihangList
             ) {
            benzhouList.add(bz);
            count++;
            if (count >= 5) {
                break;
            }
        }
        return benzhouList;
    }

    @Override
    public List<SysUser> selectSysUserList() {
        return vpaibanMapper.selectSysUserList();
    }

    @Override
    public String ContainOpenID(String openid) {
        return vpaibanMapper.ContainOpenID(openid);
    }

    @Override
    public Integer insertMiniapp(long uid, String openid) {
       return vpaibanMapper.insertMiniapp(uid,openid);
    }

    @Override
    public int updateMiniapp(long uid, String openid) {
        return vpaibanMapper.updateMiniapp(uid,openid);
    }

    @Override
    public int insertPrintLog(dayinjilu dayinjilu) {
        return vpaibanMapper.insertPrintLog(dayinjilu);
    }
}
